package br.ufpb.sistcomp.persistence;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.ufpb.sistcomp.database.InformationDatabase;
import br.ufpb.sistcomp.entity.Discipline;
import br.ufpb.sistcomp.util.Calculator;

/**
 * Class that handles the persistence of Point.
 * 
 * @author Diego Sousa - www.diegosousa.com
 * @version 1.0 Copyright (C) 2012 SistComp Empresa Jr.
 */

public class PointPersistence {
	
	private ContentValues contentValues;
	private static PointPersistence instance;
	private static SQLiteDatabase sqLiteDatabase;
	private Context context;
	private DisciplinePersistence disciplinePersistence;
	
	private PointPersistence() {
		this.contentValues = new ContentValues();
		this.disciplinePersistence = DisciplinePersistence
				.getInstance(context);		
	}

	public static synchronized PointPersistence getInstance(Context context) {

		if (instance == null) {
			instance = new PointPersistence();
			sqLiteDatabase = context.openOrCreateDatabase(
					InformationDatabase.NAME_DATABASE, Context.MODE_PRIVATE,
					null);
		}
		return instance;
	}

	public void insertPoint(Discipline discipline, double pointDiscipline) {

		Calculator calculator = new Calculator();
		Discipline disciplineAux = disciplinePersistence
				.locateDisciplines(discipline.getName());

		contentValues.put(InformationDatabase.ATRIBUTE_POINTS_ID_DISCIPLINE,
				disciplineAux.getId());
		contentValues.put(InformationDatabase.ATRIBUTE_POINTS_VALUES_POINT,
				pointDiscipline);

		sqLiteDatabase.insert(InformationDatabase.GETNAME_TABLE_POINTS, null,
				contentValues);
				
		disciplineAux.setMedia(calculator.calculatorMediaDiscipline(disciplineAux));
		disciplinePersistence.updateDisciplines(disciplineAux);
	}

	public void removePoint(Discipline discipline, double point) {

		// ps: The implementation of this method depends on the implementation of screens;
	}

	public void updatePoint() {
		
		//ps: The implementation of this method depends on the implementation of screens;

	}

	public ArrayList<Double> locatePoints(Discipline discipline) {

		ArrayList<Double> listPoints = new ArrayList<Double>();
		Discipline disciplineAux = disciplinePersistence
				.locateDisciplines(discipline.getName());

		Cursor cursorPoint = sqLiteDatabase.query(
				InformationDatabase.GETNAME_TABLE_POINTS, new String[] {
						InformationDatabase.ATRIBUTE_POINTS_ID_POINT,
						InformationDatabase.ATRIBUTE_POINTS_ID_DISCIPLINE,
						InformationDatabase.ATRIBUTE_POINTS_VALUES_POINT },
				null, null, null, null, null);

		// course is before the last line, the cursor moves to next line.
		if (cursorPoint.getPosition() == -1) {
			cursorPoint.moveToFirst();
		}
		if (cursorPoint != null) {
			while (!cursorPoint.isAfterLast()) {// Repeat until the last line.
				if (disciplineAux.getId() == cursorPoint
						.getInt(cursorPoint
								.getColumnIndex(InformationDatabase.ATRIBUTE_POINTS_ID_DISCIPLINE))) {

					listPoints
							.add((double) cursorPoint.getDouble(cursorPoint
									.getColumnIndex(InformationDatabase.ATRIBUTE_POINTS_VALUES_POINT)));

				}
				cursorPoint.moveToNext();
			}// end while.
		}
		return listPoints;
	}

	public ArrayList<Double> listAllPointsAllDisciplines(){
	
	ArrayList<Double> listPoints = new ArrayList<Double>();	
	
	Cursor cursorPoint = sqLiteDatabase.query(
			InformationDatabase.GETNAME_TABLE_POINTS, new String[] {					
					InformationDatabase.ATRIBUTE_POINTS_VALUES_POINT },
			null, null, null, null, null);

	// course is before the last line, the cursor moves to next line.
	if (cursorPoint.getPosition() == -1) {
		cursorPoint.moveToFirst();
	}

	if (cursorPoint != null) {
		while (!cursorPoint.isAfterLast()) {// repeats until the last.
			
			listPoints
			.add((double) cursorPoint.getDouble(cursorPoint
					.getColumnIndex(InformationDatabase.ATRIBUTE_POINTS_VALUES_POINT)));
			
			cursorPoint.moveToNext();			
		}
	}
	return listPoints;
}
}
